blob: dbf8d21cc739b45ebc6026df615cf10314933a37 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<script lang="ts">
import { userIdentity } from '$lib/AniList/identity.js';
import type { Badge } from '$lib/userBadgesDatabase.js';
import { onMount } from 'svelte';
import { io } from 'socket.io-client';
export let data;
let editMode = false;
let currentUserIdentity: ReturnType<typeof userIdentity>;
let error: null | string;
const socket = io();
let badges: Badge[] | null = null;
onMount(async () => {
socket.on('badges', (message) => {
badges = message;
});
if (data.user) {
currentUserIdentity = userIdentity(data.user);
socket.emit('badges', data.user);
} else {
currentUserIdentity = new Promise((resolve) =>
resolve({
name: 'Guest',
id: -1
})
);
}
});
const submitBadge = () => {
const imageURL = document.querySelector('input[name="image_url"]') as HTMLInputElement;
const activityURL = document.querySelector('input[name="activity_url"]') as HTMLInputElement;
const description = document.querySelector('input[name="description"]') as HTMLInputElement;
if (!imageURL.value || !activityURL.value) {
error = 'Fields cannot be empty.';
return;
}
if (!imageURL.value.startsWith('http') || !activityURL.value.startsWith('http')) {
error = 'URLs must start with http or https.';
return;
}
fetch(
`/api/badges/add?image=${encodeURIComponent(imageURL.value)}&post=${encodeURIComponent(
activityURL.value
)}&description=${encodeURIComponent(description.value)}`,
{
method: 'POST'
}
).then(() => {
error = null;
imageURL.value = '';
activityURL.value = '';
description.value = '';
});
};
const removeBadge = (badge: Badge) => {
fetch(`/api/badges/remove?id=${badge.id}`, {
method: 'POST'
}).then(() => {
(document.querySelector(`#badge-${badge.id}`) as HTMLAnchorElement).style.display = 'none';
});
};
</script>
{#await currentUserIdentity}
Loading ...
{:then identity}
{@const isOwner = identity && identity.name === data.username}
<p>
<a href={`/user/${data.username}`}>Back to Profile</a>
{#if isOwner}
•
<a href={`#`} on:click={() => (editMode = !editMode)}>
{editMode ? 'Disable' : 'Enable'} Edit Mode
</a>
{/if}
</p>
{#if editMode && isOwner}
<p>
Delete mode is enabled. Click on an image to delete it. There is no confirmation, so be
careful!
</p>
{#if error}
<p style="color: red;">{error}</p>
{/if}
<p>
<input type="text" placeholder="Image URL" name="image_url" minlength="1" maxlength="1000" />
<input
type="text"
placeholder="Activity URL"
name="activity_url"
minlength="1"
maxlength="1000"
/>
<input
type="text"
placeholder="Description (Optional)"
name="description"
minlength="1"
maxlength="1000"
/>
<a href={`#`} on:click={submitBadge}>Add Badge</a>
</p>
{/if}
<div id="badges">
{#if badges === null}
<p>Loading ...</p>
{:else if badges.length === 0}
No badges found for this user.
{:else}
{#each badges as badge}
{#if editMode}
<a href={`#`} on:click={() => removeBadge(badge)} id={`badge-${badge.id}`}>
<img src={badge.image} alt={badge.description} />
</a>
{:else}
<a href={badge.post} target="_blank" id={`badge-${badge.id}`}>
<img src={badge.image} alt={badge.description} />
</a>
{/if}
{/each}
{/if}
</div>
{/await}
<style>
/* body {
margin: 0;
padding: 0;
text-align: center;
background-color: #151f2e;
} */
img {
width: 100%;
height: auto;
}
#badges {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(8%, 1fr));
grid-gap: 0;
}
</style>
|